import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.apache.lucene.document.Document;



public class ExpertTest {
	//public static final String QUERY_STRING = "adwoad";
	
	public static void main(String[] args) {
		Connection myCon = makeConnection();
		
		final SqlDao my_dao = new SqlDao(myCon);
	    final Indexer my_indexer = new Indexer();
	    
	    final Scanner s = new Scanner(System.in);
	    
	    System.out.print("Query ");
	    String the_query = s.nextLine(); 
	    

	    System.out.print("Category(1 for Name, 2 for Department, 3 for Expertise) ");
	    String the_cat = s.nextLine(); 
	    
	    Map<Integer,Record> id_to_rec = new HashMap<Integer, Record>();
	    int num_cat = Integer.parseInt(the_cat);
	    
	    if(num_cat == 1) {   //If passed in Name
	    	id_to_rec = my_dao.getFacultyByName(the_query);
	    	
	    } else if (num_cat == 2) {  //passed in Dept
	    	id_to_rec = my_dao.getFacultyByDept(the_query);
	    	
	    } else if(num_cat == 3) { //passed in Expertise
	    	id_to_rec = my_dao.getFacultyByExp(the_query);

	    } 
		
		/*r_list.add(new Record(1, "ello", "http://faculty.washington.edu/yanb/"));
		//r_list.add(new Record(2,"http://faculty.washington.edu/senjutib/"));
		r_list.add(new Record(2,"bye", "http://faculty.washington.edu/adwoad/"));
		*/
	    
	    if(num_cat == 3) {
	    	
			for(final Record r: id_to_rec.values()) {
				String text = my_dao.getTextFromId(r.getID());
				my_indexer.addIndex(Integer.toString(r.getID()), text);	//send text and pic in index 
																	
			}
			//System.out.println(r_list.size());
			List<Document> my_docs = my_indexer.searchIndex(the_query);
			
			if(my_docs.isEmpty()) {
				System.out.println("No Results");
			} else {
				
			
				for(final Document d: my_docs) {
					final int id = Integer.parseInt(d.get("id"));
					final Record r = id_to_rec.get(id);
					
					System.out.println(d.get("id") + "\t" + r.getFirst_name()
									   + " " + r.getLast_name() +  "\t" + r.getLink() );
					
				}
			}
	    } else {
	    	for(final Record r: id_to_rec.values()) {
				System.out.println(r.getID() + "\t" + r.getFirst_name()
						   + " " + r.getLast_name() +  "\t" + r.getLink());
																	
			}
	    }
		
		
	}
	
	private static Connection makeConnection() {
		//Accessing Driver from JAR file
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Connection myCon = null;
		//Creates a connection with a variable called myCon
		try {
			myCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/FacultySearch",
															"root", "");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return myCon;
		
	}
}
